package com.ssm.controller;

import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ssm.model.ProductObj;
import com.ssm.service.ProductObjService;

/**
 * 实现用例的增删改查
 */
@RestController
@RequestMapping("product")
public class ProductController {
	
    private Logger logger = Logger.getLogger(ProductController.class);

    @Autowired
    private ProductObjService productObjService;
    
    @RequestMapping(value="create", method = RequestMethod.POST, consumes="application/json")
    @ResponseBody
    public Object createProduct(@RequestBody JSONObject paramJson){
    	
    	logger.info("createProduct: " + paramJson.toJSONString());
        
    	ProductObj productObj = new ProductObj();
    	
    	productObj.setName(paramJson.getString("name"));
    	productObj.setCreatdate(new Date());
    	productObj.setVersions(new JSONArray());
    	productObj.setRemark(paramJson.getString("remark"));
    	
    	return productObjService.addProduct(productObj);
    }
    
    @RequestMapping(value="delete", method = RequestMethod.POST, consumes="application/json")
    @ResponseBody
    public Object deleteProduct(@RequestBody JSONObject paramJson){
    	
    	logger.info("deleteProduct: " + paramJson.toJSONString());
        
    	return productObjService.delProduct(paramJson.getString("name"));
    }

    @RequestMapping(value="search", method = RequestMethod.POST, consumes="application/json")
    @ResponseBody
    public Object searchProduct(@RequestBody JSONObject paramJson){
    	
    	logger.info("searchProduct: " + paramJson.toJSONString());
        
    	return productObjService.searchProduct(paramJson); 
    }
    
    @RequestMapping(value="version/create", method = RequestMethod.POST, consumes="application/json")
    @ResponseBody
    public Object createProductVersion(@RequestBody JSONObject paramJson){
    	
    	logger.info("createProductVersion: " + paramJson.toJSONString());
        
    	return productObjService.addProductVersion(paramJson.getString("productname"), paramJson.getString("versionname"));
    }
    
    @RequestMapping(value="version/delete", method = RequestMethod.POST, consumes="application/json")
    @ResponseBody
    public Object deleteProductVersion(@RequestBody JSONObject paramJson){
    	
    	logger.info("deleteProductVersion: " + paramJson.toJSONString());
        
    	return productObjService.delProductVersion(paramJson.getString("productname"), paramJson.getString("versionname"));
    }

    @RequestMapping(value="version/search", method = RequestMethod.POST, consumes="application/json")
    @ResponseBody
    public Object searchProductVersion(@RequestBody JSONObject paramJson){
    	
    	logger.info("searchProductVersion: " + paramJson.toJSONString());
        
    	return productObjService.getAllVersionsByProduct(paramJson);
    }
}
